package com.tubang.logistics.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.tubang.common.constant.Constants;
import com.tubang.common.util.BeanMapper;
import com.tubang.common.util.CollectionUtils;
import com.tubang.common.util.IdGenerator;
import com.tubang.common.util.StringUtils;
import com.tubang.component.service.IAddressService;
import com.tubang.logistics.entity.*;
import com.tubang.logistics.mapper.TruckerOrderMapper;
import com.tubang.logistics.service.*;
import com.tubang.logistics.vo.ChangeStatusVo;
import com.tubang.logistics.vo.OrderBiddingVo;
import com.tubang.logistics.vo.OrderDispatchVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 运输表 服务实现类
 * </p>
 *
 * @author wsl
 * @since 2017-12-31
 */
@Service
public class TruckerOrderServiceImpl extends ServiceImpl<TruckerOrderMapper, TruckerOrderEntity> implements ITruckerOrderService {


    @Resource
    private IGoodsService goodsService;


    @Resource
    private IAddressService addressService;


    @Resource
    private IShipperOrderService shipperOrderService;


    @Resource
    private ITruckerOrderGoodsService truckerOrderGoodsService;


    @Resource
    private IPlatformOrderGoodsService platformOrderGoodsService;


    @Resource
    private ILogisticsOrderGoodsService logisticsOrderGoodsService;

    @Override
    public void updateTruckerOrderStatus(ChangeStatusVo changeStatusVo) {
        TruckerOrderEntity order = selectById(changeStatusVo.getOrderId());
        if (order == null) return;
        order.setStatusCode(changeStatusVo.getThisStatus());
        order.setStatusCodeDesc(changeStatusVo.getThisStatusDesc());
        Double carriage = changeStatusVo.getCarriage();
        if (carriage != null) {
            order.setCarriage(carriage);
        }
        String remarks = changeStatusVo.getRemarks();
        if (StringUtils.isNotEmpty(remarks)) {
            order.setRemarks(remarks);
        }
        insertOrUpdate(order);
    }

    @Override
    public void creatTruckerOrder(OrderDispatchVo orderDispatchVo) {
        Date now = new Date();
        List<String> goodsIds = orderDispatchVo.getGoodsIds();
        TruckerOrderEntity truckerOrderEntity = new TruckerOrderEntity();
        truckerOrderEntity.setEnable(true);
        insertOrUpdate(truckerOrderEntity);
        String orderId = truckerOrderEntity.getId();
        for (String goodsId : goodsIds) {
            //查询原单数据
            GoodsEntity goodsEntity = goodsService.selectById(goodsId);
            if (goodsEntity == null) continue;
            //跟新货品状态 指派中
            goodsEntity.setGoodsStatus(Constants.GOODS_WAITING_PICK_UP_GOODS);
            goodsService.insertOrUpdate(goodsEntity);
            ShipperOrderEntity shipperOrderEntity = shipperOrderService.selectById(goodsEntity.getShipperOrderId());
            //判断此商品所在订单是否有未调度的商品
            List<GoodsEntity> goodsEntities = goodsService.selectGoodssByOrderId(shipperOrderEntity.getId(), Constants.DEFAULT_VAULE_FU_ONE);
            if (CollectionUtils.isEmpty(goodsEntities)) {
                shipperOrderEntity.setStatusCode(String.valueOf(Constants.ORDER_STATUS_CODE_TAKE_GOODS));
                shipperOrderEntity.setStatusCodeDesc("待提货");
                shipperOrderService.insertOrUpdate(shipperOrderEntity);
            }
            BeanMapper.copy(shipperOrderEntity, truckerOrderEntity);
            //保存到新订单当中
            //保存关系表
            TruckerOrderGoodsEntity tko = new TruckerOrderGoodsEntity();
            tko.setOrderId(orderId);
            tko.setGoodsId(goodsId);
            tko.setCreateTime(now);
            tko.setUpdateTime(now);
            tko.setEnable(true);
            truckerOrderGoodsService.insertOrUpdate(tko);
        }
        truckerOrderEntity.setId(orderId);
        truckerOrderEntity.setStatusCode(String.valueOf(orderDispatchVo.getStatus()));
        truckerOrderEntity.setStatusCodeDesc(orderDispatchVo.getStatusCodeDesc());
        truckerOrderEntity.setCarriage(orderDispatchVo.getCarriage());
        truckerOrderEntity.setCarrierMobile(orderDispatchVo.getCarrierMobile());
        truckerOrderEntity.setCarrierName(orderDispatchVo.getCarrierName());
        truckerOrderEntity.setCarrierId(orderDispatchVo.getCarrierId());
        truckerOrderEntity.setContainerType(orderDispatchVo.getCarrierType());
        truckerOrderEntity.setUpdateTime(now);
        truckerOrderEntity.setEnable(true);
        truckerOrderEntity.setUserId(orderDispatchVo.getUserId());
        truckerOrderEntity.setUserType(orderDispatchVo.getUserType());
        truckerOrderEntity.setNum(IdGenerator.INSTANCE.nextId());
        truckerOrderEntity.setCreateTime(now);
        String goodsName = orderDispatchVo.getGoodsName();
        if (StringUtils.isNotEmpty(goodsName)) {
            truckerOrderEntity.setGoodsName(goodsName);

        }
        insertOrUpdate(truckerOrderEntity);

    }

    @Override
    public void creatTruckerOrderBeforePlatformOrder(OrderBiddingVo platformOrderBiddingVo) {
        Date now = new Date();
        //查询订单的货品
        List<PlatformOrderGoodsEntity> platformOrderGoodsEntities = platformOrderGoodsService.selectOrderByOrderId(platformOrderBiddingVo.getOrderId());
        // List<String> goodsIds = platformOrderBiddingVo.getGoodsIds();
        TruckerOrderEntity truckerOrderEntity = new TruckerOrderEntity();

        truckerOrderEntity.setId(null);
        insertOrUpdate(truckerOrderEntity);
        String orderId = truckerOrderEntity.getId();
        for (PlatformOrderGoodsEntity platformOrderGoodsEntity : platformOrderGoodsEntities) {
            //查询原单数据
            String goodsId = platformOrderGoodsEntity.getGoodsId();
            GoodsEntity goodsEntity = goodsService.selectById(goodsId);
            if (goodsEntity == null) continue;
            //跟新货品状态 指派中
            goodsEntity.setGoodsStatus(Constants.DEFAULT_VAULE_ZERO);
            goodsService.insertOrUpdate(goodsEntity);
            ShipperOrderEntity shipperOrderEntity = shipperOrderService.selectById(goodsEntity.getShipperOrderId());
            //判断此商品所在订单是否有未调度的商品
            List<GoodsEntity> goodsEntities = goodsService.selectGoodssByOrderId(shipperOrderEntity.getId(), Constants.DEFAULT_VAULE_FU_ONE);
            if (CollectionUtils.isEmpty(goodsEntities)) {
                shipperOrderEntity.setStatusCode(String.valueOf(Constants.ORDER_STATUS_CODE_TAKE_GOODS));
                shipperOrderEntity.setStatusCodeDesc("待提货");
                shipperOrderService.insertOrUpdate(shipperOrderEntity);
            }
            BeanMapper.copy(shipperOrderEntity, truckerOrderEntity);
            //保存到新订单当中
            //保存关系表
            TruckerOrderGoodsEntity tko = new TruckerOrderGoodsEntity();
            tko.setOrderId(orderId);
            tko.setGoodsId(goodsId);
            tko.setCreateTime(now);
            tko.setUpdateTime(now);
            tko.setEnable(true);
            truckerOrderGoodsService.insertOrUpdate(tko);
        }
        truckerOrderEntity.setId(orderId);
        truckerOrderEntity.setStatusCode(String.valueOf(platformOrderBiddingVo.getStatus()));
        truckerOrderEntity.setStatusCodeDesc(platformOrderBiddingVo.getStatusCodeDesc());
        truckerOrderEntity.setCarriage(platformOrderBiddingVo.getCarriage());
        truckerOrderEntity.setCarriage(platformOrderBiddingVo.getCarriage());
        truckerOrderEntity.setCarrierMobile(platformOrderBiddingVo.getCarrierMobile());
        truckerOrderEntity.setCarrierName(platformOrderBiddingVo.getCarrierName());
        truckerOrderEntity.setCarrierId(platformOrderBiddingVo.getCarrierId());
        truckerOrderEntity.setContainerType(platformOrderBiddingVo.getCarrierType());
        truckerOrderEntity.setUpdateTime(now);
        truckerOrderEntity.setCreateTime(now);
        truckerOrderEntity.setUserId(platformOrderBiddingVo.getUserId());
        truckerOrderEntity.setUserType(platformOrderBiddingVo.getUserType());
        truckerOrderEntity.setEnable(true);
        truckerOrderEntity.setNum(IdGenerator.INSTANCE.nextId());
        truckerOrderEntity.setGoodsName(platformOrderBiddingVo.getGoodsName());
        truckerOrderEntity.setGoodsWeight(platformOrderBiddingVo.getGoodsWeight());
        truckerOrderEntity.setCarriage(platformOrderBiddingVo.getCarriage());
        truckerOrderEntity.setCarriageDeal(platformOrderBiddingVo.getCarriage());
        truckerOrderEntity.setAmount(platformOrderBiddingVo.getAmount());
        truckerOrderEntity.setAmountDeal(platformOrderBiddingVo.getAmount());
        insertOrUpdate(truckerOrderEntity);
    }

    @Override
    public void creatTruckerOrderBeforLogisticsOrder(OrderBiddingVo platformOrderBiddingVo) {
        Date now = new Date();
        //查询订单的货品
        List<LogisticsOrderGoodsEntity> logisticsOrderGoodsEntities = logisticsOrderGoodsService.selectOrderByOrderId(platformOrderBiddingVo.getOrderId(), null);
        // List<String> goodsIds = platformOrderBiddingVo.getGoodsIds();
        TruckerOrderEntity truckerOrderEntity = new TruckerOrderEntity();

        truckerOrderEntity.setId(null);
        insertOrUpdate(truckerOrderEntity);
        String orderId = truckerOrderEntity.getId();
        for (LogisticsOrderGoodsEntity logisticsOrderGoodsEntity : logisticsOrderGoodsEntities) {
            //查询原单数据
            String goodsId = logisticsOrderGoodsEntity.getGoodsId();
            GoodsEntity goodsEntity = goodsService.selectById(goodsId);
            if (goodsEntity == null) continue;
            //跟新货品状态 指派中
            goodsEntity.setGoodsStatus(Constants.DEFAULT_VAULE_ZERO);
            goodsService.insertOrUpdate(goodsEntity);
            ShipperOrderEntity shipperOrderEntity = shipperOrderService.selectById(goodsEntity.getShipperOrderId());
            //判断此商品所在订单是否有未调度的商品
            List<GoodsEntity> goodsEntities = goodsService.selectGoodssByOrderId(shipperOrderEntity.getId(), Constants.DEFAULT_VAULE_FU_ONE);
            if (CollectionUtils.isEmpty(goodsEntities)) {
                shipperOrderEntity.setStatusCode(String.valueOf(Constants.ORDER_STATUS_CODE_TAKE_GOODS));
                shipperOrderEntity.setStatusCodeDesc("待提货");
                shipperOrderService.insertOrUpdate(shipperOrderEntity);
            }
            BeanMapper.copy(shipperOrderEntity, truckerOrderEntity);
            //保存到新订单当中
            //保存关系表
            TruckerOrderGoodsEntity tko = new TruckerOrderGoodsEntity();
            tko.setOrderId(orderId);
            tko.setGoodsId(goodsId);
            tko.setCreateTime(now);
            tko.setUpdateTime(now);
            tko.setEnable(true);
            truckerOrderGoodsService.insertOrUpdate(tko);
        }
        truckerOrderEntity.setId(orderId);
        truckerOrderEntity.setStatusCode(String.valueOf(platformOrderBiddingVo.getStatus()));
        truckerOrderEntity.setStatusCodeDesc(platformOrderBiddingVo.getStatusCodeDesc());
        truckerOrderEntity.setCarriage(platformOrderBiddingVo.getCarriage());
        truckerOrderEntity.setCarrierMobile(platformOrderBiddingVo.getCarrierMobile());
        truckerOrderEntity.setCarrierName(platformOrderBiddingVo.getCarrierName());
        truckerOrderEntity.setCarrierId(platformOrderBiddingVo.getCarrierId());
        truckerOrderEntity.setContainerType(platformOrderBiddingVo.getCarrierType());
        truckerOrderEntity.setUpdateTime(now);
        truckerOrderEntity.setCreateTime(now);
        truckerOrderEntity.setUserId(platformOrderBiddingVo.getUserId());
        truckerOrderEntity.setUserType(platformOrderBiddingVo.getUserType());
        truckerOrderEntity.setEnable(true);
        truckerOrderEntity.setNum(IdGenerator.INSTANCE.nextId());
        truckerOrderEntity.setGoodsName(platformOrderBiddingVo.getGoodsName());
        truckerOrderEntity.setGoodsWeight(platformOrderBiddingVo.getGoodsWeight());
        truckerOrderEntity.setCarriage(platformOrderBiddingVo.getCarriage());
        truckerOrderEntity.setCarriageDeal(platformOrderBiddingVo.getCarriage());
        truckerOrderEntity.setAmount(platformOrderBiddingVo.getAmount());
        truckerOrderEntity.setAmountDeal(platformOrderBiddingVo.getAmount());
        insertOrUpdate(truckerOrderEntity);
    }
}
